package com.han.verifycode.ver.exact;

import com.han.verifycode.CodeConfig;
import com.han.verifycode.data.FullPath;
import com.han.verifycode.data.RightKey;
import com.han.verifycode.enums.CodeLanguageEnum;
import com.han.verifycode.enums.VerifyCodeEnum;
import com.han.verifycode.utils.RandomUtil;
import com.han.verifycode.utils.StrUtil;
import com.han.verifycode.utils.DrawCodeUtil;
import com.han.verifycode.ver.ExactMatchCode;
import com.han.verifycode.ver.conf.TextConf;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;


/**
 * 简单文字验证码
 */
public class TextCode implements ExactMatchCode {
    private VerifyCodeEnum type = VerifyCodeEnum.Text;
    private TextConf thisConf;
    ////使用到Algerian字体，系统里没有的话需要安装字体，字体只显示大写，去掉了1,0,i,o几个容易混淆的字符
    private static final String NUM_STR = "23456789";
    private static final String EN_STR = "ABCDEFGHJKMNPQRSTUVWXYZ";

    public RightKey generate(String code, CodeConfig conf) {
        RightKey key = new RightKey(code);
        if (conf == null) {
            thisConf = new TextConf();
        } else {
            thisConf = (TextConf) conf;
        }
        key.setConfig(thisConf);
        key.setType(type);
        key.setTips("请输入图片中的文字");
        key.setWidth(conf.getWidth());
        key.setHeight(conf.getHeight());
        String ans = generateAnswer(thisConf);
        key.setAnswer(ans);
        FullPath fullPath = generateFilePath(thisConf.getBasePath(), key.getCode());
        key.setFullPath(fullPath);
        //画图
        draw(key);
        return key;
    }


    private String generateAnswer(TextConf conf) {
        if (conf.getLang() == CodeLanguageEnum.en) {
            Random r = RandomUtil.getRandom();
            int length = conf.getLength();
            String sourceStr = conf.isHasNum() ? NUM_STR : "";
            sourceStr += conf.isHasLetter() ? EN_STR : "";
            if (sourceStr.trim().length() == 0) {
                throw new RuntimeException("没有可以准备的数据");
            }
            int maxLength = sourceStr.length();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < length; i++) {
                int index = r.nextInt(maxLength - 1);
                sb.append(sourceStr.charAt(index));
            }
            return sb.toString();
        }
        //中文
        return StrUtil.getRandomIdiom(thisConf.getIdiomSourceFile());
    }

    /**
     * 调用画图
     *
     * @param key
     */
    private void draw(RightKey key) {
        String filePath=key.getFullPath().getAbsolutePath();

        FileOutputStream f = null;
        try {
            f = new FileOutputStream(new File(filePath));
            DrawCodeUtil.outputImage(thisConf.getWidth().intValue(), thisConf.getHeight().intValue(), f, key.getAnswer(), thisConf.isSalt(), true);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (f != null) {
                try {
                    f.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    @Override
    public CodeConfig getConfig() {
        return thisConf;
    }

}
